﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Oplaco.Core.BusinessLogicLayer;
using Oplaco.Web.DTO;
using Oplaco.Web.UI;


public partial class ShowAd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //get all query params
            int adId;
            string adIdQs = Request.QueryString["id"];
            if (adIdQs != null && !Int32.TryParse(adIdQs, out adId))
            {
                Response.Redirect("~/Search.aspx");
            }

            //get all data
            CachedAdvertisment ad = AdvertismentManager.FetchSingle(HttpContext.Current, Int32.Parse(adIdQs));
            CachedCategory cat = CategoryManager.FetchSingle(ad.CategoryId);
            AdType adtype = (AdType)Enum.Parse(typeof(AdType), ad.AdvertismentType, false);

            //set page title for SEO
            Oplaco.Core.Configuration.SiteSettings settings = Oplaco.Core.Configuration.SiteSettings.GetSharedSettings();
            Page.Title = settings.SiteName + " | " + ad.CategoryPath + " : " + ad.Title;


            //handle screen data
            lblTitle.Text = ad.Title;
            lblCategoryTitle.Text = ad.CategoryPath;
            lblDescription.Text = ad.Description;
            txtPrice.Text = ad.Price.ToString();
            lblAdType.Text = ad.AdvertismentType;

            MembersDB Member = new MembersDB();
            Member.Initialize(ad.MemberId);
            if (Member.GIS != null)
            {
                lblLocation.Visible = true;
                txtLocation.Text = Member.GIS.PostalCode.ToString() + Member.GIS.PostalExtension;
            }

            if (!String.IsNullOrEmpty(ad.Url))
            {
                hlUrl.NavigateUrl = Server.HtmlDecode(ad.Url);
                hlUrl.Text = "external link";
                hlUrl.Visible = true;
            }

            if (ad.MemberId > 0) {

                MembersDB MDB = new MembersDB();
                
                    MDB.Initialize(ad.MemberId);

                    lblUserNickname.Visible = true;
                    hlNickname.Text = MDB.Nickname;
                
            }

            int[] imgs = ImageManager.FetchImageIdentifiers(HttpContext.Current, ad.Id);
            if (imgs != null && imgs.Count() > 0)
            {
                RepeaterImages.DataSource = imgs;
                RepeaterImages.DataBind();
            }

            //fill viewstate bag
            ViewState.Add("adId", ad.Id);
            ViewState.Add("cId", ad.CategoryId);
            ViewState.Add("mId", ad.MemberId);

        }
        catch (Oplaco.Exceptions.BusinessLayerException BLE)
        {
            lblMessage.Text = ConfigurationManager.AppSettings["GenericErrorMessage"];
            lblMessage.Visible = true;
            lblMessage.CssClass = "error";
            Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(BLE);
        }
        catch (Exception exception) {
            lblMessage.Text = ConfigurationManager.AppSettings["GenericErrorMessage"];
            lblMessage.Visible = true;
            lblMessage.CssClass = "error";
            Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(exception);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e) {

        try
        {
            
            int mid = (Int32)ViewState["mId"];
            string fromemail = String.Empty;
            if (User.IsInRole("Users") && User.Identity.IsAuthenticated)
            {
                fromemail = Profile.Email;

            }
            else
            {
                TextBox txtFrom = (TextBox)LoginView1.Controls[0].Controls[0].FindControl("txtEmailFrom");
                fromemail = txtFrom.Text;
                txtFrom.Text = String.Empty;
            }

            string message = "The following message from: " + fromemail + " was send to you.\n" + txtMessage.Text;
            Mail.Send(mid, "os.Oplaco message", Server.HtmlEncode(message));

            lblMessage.Text = "Success, your message has been sent";
            lblMessage.CssClass = "success";
            lblMessage.Visible = true;
            txtMessage.Text = String.Empty;
        }
        catch (Oplaco.Exceptions.BusinessLayerException BLE)
        {
            lblMessage.Text = ConfigurationManager.AppSettings["GenericErrorMessage"];
            lblMessage.Visible = true;
            lblMessage.CssClass = "error";
            Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(BLE);
        }
        catch (Exception exception)
        {
            lblMessage.Text = ConfigurationManager.AppSettings["GenericErrorMessage"];
            lblMessage.Visible = true;
            lblMessage.CssClass = "error";
            Oplaco.Exceptions.AppService.ProcessAndNotifyPresentationLayerException(exception);
        }
        

    }
}